package com.heer.gymnasium.adapter;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.heer.gymnasium.R;
import com.heer.gymnasium.activity.MainActivity;
import com.heer.gymnasium.constant.Type;
import com.heer.gymnasium.dialog.QRAccessDialog;
import com.heer.gymnasium.util.LoadImageUtil;
import com.heer.gymnasium.widget.MyListView;
import com.heer.model.MyInvitedBean;
import com.heer.model.OrderBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by rzw2 on 2017/4/5.
 */

public class InvitationCarAdapter extends ListAdapter<MyInvitedBean> {


    public InvitationCarAdapter(Activity activity) {
        super(activity);
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        ViewHolder holder;
        if (view != null) {
            holder = (ViewHolder) view.getTag();
        } else {
            view = mInflater.inflate(R.layout.item_order_list, parent, false);
            holder = new ViewHolder(view);
            view.setTag(holder);
        }

        initOrderView(holder, getItem(position));
        return view;
    }

    /**
     * @param holder
     * @param data
     */
    private void initOrderView(final ViewHolder holder, final MyInvitedBean data) {
        if (null != data.getTicket())
            switch (data.getTicket().getCat_id()) {
                case Type.SITE_TICKET_TYPE_1://1:门票
                case Type.SITE_TICKET_TYPE_2://2:次卡
                case Type.SITE_TICKET_TYPE_3://3:期卡
                    holder.layoutSite.setVisibility(View.GONE);
                    holder.layoutCard.setVisibility(View.VISIBLE);
                    holder.tvTitle.setText(data.getStadium().getStadiumname() + "($1)".replace("$1", data.getTicket().getCat_id() == Type.SITE_TICKET_TYPE_1 ? "门票" : data.getTicket().getCat_id() == Type.SITE_TICKET_TYPE_2 ? "次卡" : "期卡"));
                    holder.tvSite.setText(data.getStadium().getStadiumname());
                    Glide.with(mActivity)
                            .load(LoadImageUtil.getLoadImageUrl(data.getPlace().getImage()))
                            .placeholder(R.mipmap.ic_order_card)
                            .into(holder.imageCard);
                    break;
                case Type.SITE_TICKET_TYPE_4://4:包场
                    holder.layoutSite.setVisibility(View.VISIBLE);
                    holder.layoutCard.setVisibility(View.GONE);

                    holder.tvTitle.setText(data.getStadium().getStadiumname());
                    holder.btn1.setVisibility(View.VISIBLE);
                    final OrderSiteTypeAdapter mAdapter = new OrderSiteTypeAdapter(this.mActivity,data.getTicket().getImage());
                    holder.lvSite.setAdapter(mAdapter);

                    List<OrderBean.Booking> bookings = new ArrayList<>();
                    for (String[] array : data.getTimes()) {
                        OrderBean.Booking book = new OrderBean().new Booking();
                        book.setStime(array[0]);
                        book.setEtime(array[1]);
                        book.setTitle(data.getPlace().getTitle());
                        bookings.add(book);
                    }

                    mAdapter.setItems(bookings);
                    mAdapter.setCount(1);
                    holder.imageArrow.setImageResource(R.mipmap.arrow_down);
                    if (data.getTimes().size() > 1)
                        holder.imageArrow.setVisibility(View.VISIBLE);
                    else
                        holder.imageArrow.setVisibility(View.GONE);
                    holder.layoutSite.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (mAdapter.getCount() == 1) {
                                mAdapter.setCount(mAdapter.getItemList().size());
                                holder.imageArrow.setImageResource(R.mipmap.arrow_top);
                            } else {
                                mAdapter.setCount(1);
                                holder.imageArrow.setImageResource(R.mipmap.arrow_down);
                            }
                        }
                    });
                    break;
            }

        holder.layoutAddress.setVisibility(View.GONE);
        holder.btn1.setVisibility(View.GONE);
        holder.btn2.setVisibility(View.VISIBLE);
        holder.tvTitle.setGravity(Gravity.CENTER);
        holder.btnShare.setVisibility(View.GONE);

        if (TextUtils.equals(data.getOrder().getAccess_status(), "0")) {
            holder.btn2.setText("入场券");
           holder.btn2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                   Intent intent=new Intent(mActivity, MainActivity.class);
                    intent.putExtra("index",2);
                    mActivity.startActivity(intent);
                    mActivity.finish();
                }
            });
        } else {
            holder.btn2.setText("出场券");
            holder.btn2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(mActivity, "出场券", Toast.LENGTH_SHORT).show();
                    new QRAccessDialog(mActivity, data.getOrder().getOrder_sn()).show();
                }
            });
        }

    }

    static class ViewHolder {
        @BindView(R.id.tv_title)
        TextView tvTitle;
        @BindView(R.id.btn_share)
        TextView btnShare;
        @BindView(R.id.image_card)
        ImageView imageCard;
        @BindView(R.id.tv_site)
        TextView tvSite;
        @BindView(R.id.tv_status)
        TextView tvStatus;
        @BindView(R.id.tv_status1)
        TextView tvStatus1;
        @BindView(R.id.layout_address)
        LinearLayout layoutAddress;
        @BindView(R.id.layout_card)
        LinearLayout layoutCard;
        @BindView(R.id.lv_site)
        MyListView lvSite;
        @BindView(R.id.image_arrow)
        ImageView imageArrow;
        @BindView(R.id.layout_site)
        RelativeLayout layoutSite;
        @BindView(R.id.btn_1)
        TextView btn1;
        @BindView(R.id.btn_2)
        TextView btn2;
        @BindView(R.id.layout_item)
        LinearLayout layoutItem;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}